package top.goldenyear.porpoise.framework.orm.config;

import com.easy.query.core.configuration.QueryConfiguration;
import com.easy.query.solon.annotation.Db;
import com.zaxxer.hikari.HikariDataSource;
import javax.sql.DataSource;
import lombok.extern.slf4j.Slf4j;
import org.noear.solon.annotation.Bean;
import org.noear.solon.annotation.Configuration;
import org.noear.solon.annotation.Inject;
import top.goldenyear.porpoise.framework.orm.core.converter.JsonConverter;
import top.goldenyear.porpoise.framework.orm.core.interceptor.ModelInterceptor;

/**
 * @author airhead
 */
@Configuration
@Slf4j
public class EasyQueryConfig {

  @Bean(name = "master", typed = true)
  public DataSource masterDb(@Inject("${porpoise.datasource.master}") HikariDataSource dataSource) {

    return dataSource;
  }

  @Bean
  public void dbMasterQueryConfiguration(
      @Db QueryConfiguration configuration, @Inject JsonConverter jsonConverter) {
    configuration.applyValueConverter(jsonConverter);
    configuration.applyInterceptor(new ModelInterceptor());
  }

  @Bean(typed = true)
  public JsonConverter jsonConverter() {
    return new JsonConverter();
  }
}
